package com.log530_projetfinal;

import com.log530_projetfinal.CommonAction.AttCarnivore;
import com.log530_projetfinal.CommonAction.AttHerbivore;
import com.log530_projetfinal.CommonAction.AttVegetale;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;

public class MainActivity extends Activity {
	LinearLayout layout;
	DrawView dv;

	private World world;

	Button btnPlayStop;
	Button btnInit;
	Button btnDefault;

	int nbPlantes = 0;

	private int mRequestCode = 100;

	private AttVegetale attVegetale;
	private AttHerbivore attHerbivore;
	private AttCarnivore attCarnivore;

	private int statusPlayStop = 0;
	private int statusInit = 0;
	
	private SharedPreferences sharedPref;
	
	private Handler myHandler = new Handler();
	private Runnable myRunnable = new Runnable() {

		public void run() {
			world.worldStep();
			dv.invalidate();
			afficher();
		}

	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		layout = (LinearLayout) findViewById(R.id.canvas);
		dv = new DrawView(this);
		layout.addView(dv);

		btnPlayStop = (Button) findViewById(R.id.playStop);
		btnInit = (Button) findViewById(R.id.init);
		btnDefault = (Button) findViewById(R.id.defaults);

		btnPlayStop.setText("Stop");

		attVegetale = new AttVegetale();
		attHerbivore = new AttHerbivore();
		attCarnivore = new AttCarnivore();
		
		sharedPref = getSharedPreferences("TEST", Context.MODE_PRIVATE);

		world = new World();

		btnPlayStop.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View view) {

				if (statusPlayStop == 1) {
					myHandler.removeCallbacks(myRunnable);
					btnPlayStop.setText("Play");
					statusPlayStop = 0;
				} else {
					btnPlayStop.setText("Stop");
					statusPlayStop = 1;
					myRunnable.run();
				}

			}

		});

		btnDefault.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View view) {

				btnPlayStop.setText("Stop");

				if (statusInit == 0) {
					statusInit = 1;
				} else {
					myHandler.removeCallbacks(myRunnable);
				}
				world.destroyWorld();
				world.setAttributAll();
				world.initWorld();
				myRunnable.run();

				statusPlayStop = 1;
			}

		});

		btnInit.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View view) {

				myHandler.removeCallbacks(myRunnable);
				world.destroyWorld();
				btnPlayStop.setText("Stop");
				statusPlayStop = 1;
				
				world.setAttributVegetale(sharedPref.getInt("nbPlantes", 80),
						sharedPref.getInt("pointViePlante", 100),
						sharedPref.getInt("maturitePlante", 1000), 
						sharedPref.getInt("vitalitePlante", 20),
						sharedPref.getInt("dimensionPlante", 8),
						sharedPref.getInt("boisementPlante", 7));
				
				world.setAttributHerbivore(sharedPref.getInt("nbLapins", 15),
						sharedPref.getInt("pointVieLapin", 100),
						sharedPref.getInt("maturiteLapin", 10),
						sharedPref.getInt("vitaliteLapin", 5), 
						sharedPref.getInt("dimensionLapin", 8),
						sharedPref.getInt("visionLapin", 50), 
						sharedPref.getInt("odoratLapin", 100), 
						150, 25, 25, 5, 2, 5, 500, 1);
				
				world.setAttributCarnivore(
						sharedPref.getInt("nbLoups", 5),
						sharedPref.getInt("pointVieLoup", 100),
						sharedPref.getInt("maturiteLoup", 20),
						sharedPref.getInt("vitaliteLoup", 5), 
						sharedPref.getInt("dimensionLoup",13),
						sharedPref.getInt("visionLoup", 30), 
						sharedPref.getInt("odoratLoup", 90), 
						150, 30, 25, 7, 2, 7, 500, 1);
				
				world.initWorld();
				myRunnable.run();
			}

		});
	}

	public void afficher() {
		myHandler.postDelayed(myRunnable, 50);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menu_settings:
			// Comportement du bouton "Parametres"
			Intent intent = new Intent(MainActivity.this, SettingsActivity.class);
			startActivityForResult(intent, mRequestCode);
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == mRequestCode && resultCode == RESULT_OK) {
			System.out.println("receive from param: ");
			attVegetale.setNbPlante(data.getStringExtra("plante1") == null ? world.attVege.getNbPlante() : Integer.parseInt(data.getStringExtra("plante1")));
			attVegetale.setPtViePlante(data.getStringExtra("plante2") == null ? world.attVege.getPtViePlante() : Integer.parseInt(data.getStringExtra("plante2")));
			attVegetale.setAgeReproduction(data.getStringExtra("plante3") == null ? world.attVege.getAgeReproduction() : Integer.parseInt(data.getStringExtra("plante3")));
			attVegetale.setVitalitePlante(data.getStringExtra("plante4") == null ? world.attVege.getVitalitePlante() : Integer.parseInt(data.getStringExtra("plante4")));
			
			attVegetale.setSize(data.getStringExtra("plante5") == null ? world.attVege.getSize() : Integer.parseInt(data.getStringExtra("plante5")));
			attVegetale.setBoisement(data.getStringExtra("plante6") == null ? world.attVege.getBoisement() : Integer.parseInt(data.getStringExtra("plante6")));

			
			attHerbivore.setNbHerbivore(data.getStringExtra("lapin1") == null ? world.attHerbi.nbHerbivore : Integer.parseInt(data.getStringExtra("lapin1")));
			attHerbivore.setPtVie(data.getStringExtra("lapin2") == null ? world.attHerbi.ptVie : Integer.parseInt(data.getStringExtra("lapin2")));
			attHerbivore.setAgeReproduction(data.getStringExtra("lapin3") == null ? world.attHerbi.ageReproduction : Integer.parseInt(data.getStringExtra("lapin3")));
			attHerbivore.setVitalite(data.getStringExtra("lapin4") == null ? world.attHerbi.vitalite : Integer.parseInt(data.getStringExtra("lapin4")));

			attHerbivore.setSize(data.getStringExtra("lapin5") == null ? world.attHerbi.size : Integer.parseInt(data.getStringExtra("lapin5")));
			attHerbivore.setVision(data.getStringExtra("lapin6") == null ? world.attHerbi.vision : Integer.parseInt(data.getStringExtra("lapin6")));
			attHerbivore.setOdorat(data.getStringExtra("lapin7") == null ? world.attHerbi.odorat : Integer.parseInt(data.getStringExtra("lapin7")));

			
			attCarnivore.setNbCarnivore(data.getStringExtra("loup1") == null ? world.attCarni.nbCarnivore : Integer.parseInt(data.getStringExtra("loup1")));
			attCarnivore.setPtVie(data.getStringExtra("loup2") == null ? world.attCarni.ptVie : Integer.parseInt(data.getStringExtra("loup2")));
			attCarnivore.setAgeReproduction(data.getStringExtra("loup3") == null ? world.attCarni.ageReproduction : Integer.parseInt(data.getStringExtra("loup3")));
			attCarnivore.setVitalite(data.getStringExtra("loup4") == null ? world.attCarni.vitalite : Integer.parseInt(data.getStringExtra("loup4")));

			attCarnivore.setSize(data.getStringExtra("loup5") == null ? world.attCarni.size : Integer.parseInt(data.getStringExtra("loup5")));
			attCarnivore.setVision(data.getStringExtra("loup6") == null ? world.attCarni.vision : Integer.parseInt(data.getStringExtra("loup6")));
			attCarnivore.setOdorat(data.getStringExtra("loup7") == null ? world.attCarni.odorat : Integer.parseInt(data.getStringExtra("loup7")));
		}
	}

}
